package com.zrf.base.knowledge.executor;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class ExecutorServiceTest {

    public static void main(String[] args) throws InterruptedException {
        testCacheThreadPoll();
    }

    public static void testCacheThreadPoll() throws InterruptedException {
        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i = 0; i < 10; i++) {
            TimeUnit.MICROSECONDS.sleep(10);
            int finalI = i;
            executorService.execute(()->{
                Thread thread = Thread.currentThread();
                System.out.println("index:" + finalI + ", threadId:" + thread.getId());
            });
        }
        if (!executorService.isShutdown()) {
            executorService.shutdown();
        }
    }
}
